<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Pages.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_Model_DbTable_Pages extends Engine_Db_Table {
	
	protected $_rowClass = "Sitepage_Model_Page";
	
	/**
	 * Get pages to add as item of the day
	 * @param string $title : search text
	 * @param int $limit : result limit
	 */
	public function getDayItems($title, $limit = 10) {
		
		//MAKE QUERY
		$select = $this->select ()->from ( $this->info ( 'name' ), array ('page_id', 'owner_id', 'title', 'photo_id' ) )->where ( 'title  LIKE ? ', '%' . $title . '%' )->where ( 'closed = ?', '0' )->where ( 'declined = ?', '0' )->where ( 'approved = ?', '1' )->where ( 'draft = ?', '1' )->order ( 'title ASC' )->limit ( $limit );
		
		//FETCH RESULTS
		return $this->fetchAll ( $select );
	}
	
	public function getTagCloud($limit = 100) {
		
		$tableTagmaps = 'engine4_core_tagmaps';
		$tableTags = 'engine4_core_tags';
		
		$tableSitepages = $this->info ( 'name' );
		$select = $this->select ()->setIntegrityCheck ( false )->from ( $tableSitepages, 'title' )->joinInner ( $tableTagmaps, "$tableSitepages.page_id = $tableTagmaps.resource_id", array ('COUNT(engine4_core_tagmaps.resource_id) AS Frequency' ) )->joinInner ( $tableTags, "$tableTags.tag_id = $tableTagmaps.tag_id", array ('text', 'tag_id' ) )->where ( $tableSitepages . '.approved = ?', "1" );
		$stusShow = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.status.show', 1 );
		
		if ($stusShow == 0) {
			$select = $select->where ( $tableSitepages . '.closed = ?', "0" );
		}
		
		if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ())
			$select->where ( $tableSitepages . '.expiration_date  > ?', date ( "Y-m-d H:i:s" ) );
		
		$select->where ( $tableSitepages . ".search = ?", 1 );
		$select = $select->where ( $tableSitepages . '.draft = ?', "1" )->where ( $tableSitepages . '.declined = ?', '0' )->where ( $tableTagmaps . '.resource_type = ?', 'sitepage_page' )->group ( "$tableTags.text" )->order ( "Frequency DESC" )->limit ( $limit );
		
		//Start Network work
		$enableNetwork = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.network', 0 );
		if (! empty ( $enableNetwork )) {
			$viewer = Engine_Api::_ ()->user ()->getViewer ();
			$networkMembershipTable = Engine_Api::_ ()->getDbtable ( 'membership', 'network' );
			if (! Engine_Api::_ ()->getApi ( 'subCore', 'sitepage' )->pageBaseNetworkEnable ()) {
				$viewerNetwork = $networkMembershipTable->fetchRow ( array ('user_id = ?' => $viewer->getIdentity () ) );
				
				if (! empty ( $viewerNetwork )) {
					
					$networkMembershipName = $networkMembershipTable->info ( 'name' );
					$select->join ( $networkMembershipName, "`{$tableSitepages}`.owner_id = `{$networkMembershipName}`.user_id  ", null )->join ( $networkMembershipName, "`{$networkMembershipName}`.`resource_id`=`{$networkMembershipName}_2`.resource_id", null )->where ( "`{$networkMembershipName}_2`.user_id = ? ", $viewer->getIdentity () );
				}
			} else {
				$viewerNetwork = $networkMembershipTable->getMembershipsOfInfo ( $viewer );
				$str = array ();
				$columnName = "`{$tableSitepages}`.networks_privacy";
				foreach ( $viewerNetwork as $networkvalue ) {
					$network_id = $networkvalue->resource_id;
					$str [] = "'" . $network_id . "'";
					$str [] = "'" . $network_id . ",%'";
					$str [] = "'%," . $network_id . ",%'";
					$str [] = "'%," . $network_id . "'";
				}
				if (! empty ( $str )) {
					$likeNetworkVale = ( string ) (join ( " or $columnName  LIKE ", $str ));
					$select->where ( $columnName . ' LIKE ' . $likeNetworkVale . ' or ' . $columnName . " IS NULL" );
				} else {
					$select->where ( $columnName . " IS NULL" );
				}
			}
		}
		//End Network work
		

		return $select->query ()->fetchAll ();
	}
	
	/**
	 * Return pages which have this category and this mapping
	 *
	 * @param int category_id
	 * @param int profile_type
	 * @return Zend_Db_Table_Select
	 */
	public function getCategoryPage($category_id, $profile_type) {
		$select = $this->select ()->from ( $this->info ( 'name' ), 'page_id' )->where ( 'category_id = ?', $category_id )->where ( 'profile_type != ?', $profile_type );
		return $this->fetchAll ( $select )->toArray ();
	}
	
	/**
	 * Return pages which can user have to choice to claim
	 *
	 * @param array params
	 * @return Zend_Db_Table_Select
	 */
	public function getSuggestClaimPage($params) {
		//SELECT
		$select = $this->select ()->from ( $this->info ( 'name' ), array ('page_id', 'title', 'userclaim', 'photo_id', 'owner_id' ) )->where ( 'approved = ?', '1' )->where ( 'declined = ?', '0' )->where ( 'draft = ?', '1' );
		
		if (isset ( $params ['page_id'] ) && ! empty ( $params ['page_id'] )) {
			$select = $select->where ( 'page_id = ?', $params ['page_id'] );
		}
		
		if (isset ( $params ['viewer_id'] ) && ! empty ( $params ['viewer_id'] )) {
			$select = $select->where ( 'owner_id != ?', $params ['viewer_id'] );
		}
		
		if (isset ( $params ['title'] ) && ! empty ( $params ['title'] )) {
			$select = $select->where ( 'title LIKE ? ', '%' . $params ['title'] . '%' );
		}
		
		if (isset ( $params ['limit'] ) && ! empty ( $params ['limit'] )) {
			$select = $select->limit ( $params ['limit'] );
		}
		
		if (isset ( $params ['orderby'] ) && ! empty ( $params ['orderby'] )) {
			$select = $select->order ( $params ['orderby'] );
		}
		$stusShow = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.status.show', 1 );
		if ($stusShow == 0) {
			$select = $select->where ( 'closed = ?', '0' );
		}
		if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ())
			$select->where ( 'expiration_date  > ?', date ( "Y-m-d H:i:s" ) );
		
		//FETCH
		return $this->fetchAll ( $select );
	}
	
	/**
	 * Get Popular location base on city and state
	 *
	 */
	public function getPopularLocation() {
		$limit = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.popular.locations', 10 );
		$pageName = $this->info ( 'name' );
		$locationTable = Engine_Api::_ ()->getDbtable ( 'locations', 'sitepage' );
		$locationName = $locationTable->info ( 'name' );
		$select = $this->select ()->setIntegrityCheck ( false )->from ( $pageName, null )->join ( $locationName, "$pageName.page_id = $locationName.page_id", array ("city", "count(city) as count_location", "state", "count(state) as count_location_state" ) )->group ( "city" )->group ( "state" )->order ( "count_location DESC" )->limit ( $limit );
		
		$select->where ( $pageName . '.approved = ?', '1' )->where ( $pageName . '.declined = ?', '0' )->where ( $pageName . '.draft = ?', '1' );
		$select->where ( $pageName . ".search = ?", 1 );
		$stusShow = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.status.show', 1 );
		if ($stusShow == 0) {
			$select = $select->where ( $pageName . '.closed = ?', '0' );
		}
		if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ())
			$select->where ( $pageName . '.expiration_date  > ?', date ( "Y-m-d H:i:s" ) );
		
		//Start Network work
		$enableNetwork = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.network', 0 );
		if (! empty ( $enableNetwork )) {
			$viewer = Engine_Api::_ ()->user ()->getViewer ();
			$networkMembershipTable = Engine_Api::_ ()->getDbtable ( 'membership', 'network' );
			if (! Engine_Api::_ ()->getApi ( 'subCore', 'sitepage' )->pageBaseNetworkEnable ()) {
				$viewerNetwork = $networkMembershipTable->fetchRow ( array ('user_id = ?' => $viewer->getIdentity () ) );
				
				if (! empty ( $viewerNetwork )) {
					
					$networkMembershipName = $networkMembershipTable->info ( 'name' );
					$select->join ( $networkMembershipName, "`{$pageName}`.owner_id = `{$networkMembershipName}`.user_id  ", null )->join ( $networkMembershipName, "`{$networkMembershipName}`.`resource_id`=`{$networkMembershipName}_2`.resource_id", null )->where ( "`{$networkMembershipName}_2`.user_id = ? ", $viewer->getIdentity () );
				}
			} else {
				$viewerNetwork = $networkMembershipTable->getMembershipsOfInfo ( $viewer );
				$str = array ();
				$columnName = "`{$pageName}`.networks_privacy";
				foreach ( $viewerNetwork as $networkvalue ) {
					$network_id = $networkvalue->resource_id;
					$str [] = "'" . $network_id . "'";
					$str [] = "'" . $network_id . ",%'";
					$str [] = "'%," . $network_id . ",%'";
					$str [] = "'%," . $network_id . "'";
				}
				if (! empty ( $str )) {
					$likeNetworkVale = ( string ) (join ( " or $columnName  LIKE ", $str ));
					$select->where ( $columnName . ' LIKE ' . $likeNetworkVale . ' or ' . $columnName . " IS NULL" );
				} else {
					$select->where ( $columnName . " IS NULL" );
				}
			}
		}
		//End Network work
		

		return $this->fetchAll ( $select );
	}
	
	/**
	 * Get Arcive Pages
	 *
	 * @param int $user_id
	 * @return object
	 */
	public function getArchiveSitepage($user_id = null) {
		
		$rName = $this->info ( 'name' );
		$select = $this->select ()->setIntegrityCheck ( false )->from ( $rName )->where ( $rName . '.closed = ?', '0' )->where ( $rName . '.approved = ?', '1' )->where ( $rName . '.declined = ?', '0' )->where ( $rName . '.draft = ?', '1' );
		$select->where ( $rName . ".search = ?", 1 );
		if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ())
			$select->where ( $rName . '.expiration_date  > ?', date ( "Y-m-d H:i:s" ) );
		;
		
		if (! empty ( $params ['user_id'] ) && is_numeric ( $params ['user_id'] )) {
			$select->where ( $rName . '.owner_id = ?', $params ['user_id'] );
		}
		
		//Start Network work
		$enableNetwork = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.network', 0 );
		if (! empty ( $enableNetwork )) {
			$viewer = Engine_Api::_ ()->user ()->getViewer ();
			$networkMembershipTable = Engine_Api::_ ()->getDbtable ( 'membership', 'network' );
			if (! Engine_Api::_ ()->getApi ( 'subCore', 'sitepage' )->pageBaseNetworkEnable ()) {
				$viewerNetwork = $networkMembershipTable->fetchRow ( array ('user_id = ?' => $viewer->getIdentity () ) );
				if (! empty ( $viewerNetwork )) {
					$networkMembershipName = $networkMembershipTable->info ( 'name' );
					$select->join ( $networkMembershipName, "`{$rName}`.owner_id = `{$networkMembershipName}`.user_id  ", null )->join ( $networkMembershipName, "`{$networkMembershipName}`.`resource_id`=`{$networkMembershipName}_2`.resource_id", null )->where ( "`{$networkMembershipName}_2`.user_id = ? ", $viewer->getIdentity () );
				}
			} else {
				$viewerNetwork = $networkMembershipTable->getMembershipsOfInfo ( $viewer );
				$str = array ();
				$columnName = "`{$rName}`.networks_privacy";
				foreach ( $viewerNetwork as $networkvalue ) {
					$network_id = $networkvalue->resource_id;
					$str [] = "'" . $network_id . "'";
					$str [] = "'" . $network_id . ",%'";
					$str [] = "'%," . $network_id . ",%'";
					$str [] = "'%," . $network_id . "'";
				}
				if (! empty ( $str )) {
					$likeNetworkVale = ( string ) (join ( " or $columnName  LIKE ", $str ));
					$select->where ( $columnName . ' LIKE ' . $likeNetworkVale . ' or ' . $columnName . " IS NULL" );
				} else {
					$select->where ( $columnName . " IS NULL" );
				}
			}
		}
		//END NETWORK WORK
		return $this->fetchAll ( $select );
	}
	
	/**
	 * Get Pages relative to page owner
	 *
	 * @param int $page_id
	 * @param int $owner_id
	 * @return objects
	 */
	public function userPage($owner_id, $page_id) {
		
		$rName = $this->info ( 'name' );
		$select = $this->select ()->where ( $rName . '.closed = ?', '0' )->where ( $rName . '.approved = ?', '1' )->where ( $rName . '.declined = ?', '0' )->where ( $rName . '.draft = ?', '1' )->where ( $rName . ".search = ?", 1 )->where ( $rName . '.page_id <> ?', $page_id )->where ( $rName . '.owner_id = ?', $owner_id );
		
		if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ())
			$select->where ( $rName . '.expiration_date  > ?', date ( "Y-m-d H:i:s" ) );
		
		$limit = ( int ) Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.usersitepage.widgets', 3 );
		
		//Start Network work
		$enableNetwork = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.network', 0 );
		if (! empty ( $enableNetwork )) {
			$viewer = Engine_Api::_ ()->user ()->getViewer ();
			$networkMembershipTable = Engine_Api::_ ()->getDbtable ( 'membership', 'network' );
			if (! Engine_Api::_ ()->getApi ( 'subCore', 'sitepage' )->pageBaseNetworkEnable ()) {
				$viewerNetwork = $networkMembershipTable->fetchRow ( array ('user_id = ?' => $viewer->getIdentity () ) );
				
				if (! empty ( $viewerNetwork )) {
					$select->setIntegrityCheck ( false )->from ( $rName );
					$networkMembershipName = $networkMembershipTable->info ( 'name' );
					$select->join ( $networkMembershipName, "`{$rName}`.owner_id = `{$networkMembershipName}`.user_id  ", null )->join ( $networkMembershipName, "`{$networkMembershipName}`.`resource_id`=`{$networkMembershipName}_2`.resource_id", null )->where ( "`{$networkMembershipName}_2`.user_id = ? ", $viewer->getIdentity () );
				}
			} else {
				$viewerNetwork = $networkMembershipTable->getMembershipsOfInfo ( $viewer );
				$str = array ();
				$columnName = "`{$rName}`.networks_privacy";
				foreach ( $viewerNetwork as $networkvalue ) {
					$network_id = $networkvalue->resource_id;
					$str [] = "'" . $network_id . "'";
					$str [] = "'" . $network_id . ",%'";
					$str [] = "'%," . $network_id . ",%'";
					$str [] = "'%," . $network_id . "'";
				}
				if (! empty ( $str )) {
					$likeNetworkVale = ( string ) (join ( " or $columnName  LIKE ", $str ));
					$select->where ( $columnName . ' LIKE ' . $likeNetworkVale . ' or ' . $columnName . " IS NULL" );
				} else {
					$select->where ( $columnName . " IS NULL" );
				}
			}
		}
		//End Network work
		$select = $select->limit ( $limit );
		
		return $this->fetchALL ( $select );
	}
	
	/**
	 * Get Pages for links
	 *
	 * @param int $page_id
	 * @param int $viewer_id
	 * @return objects
	 */
	public function getPages($page_id, $viewer_id) {
		
		$sitepageName = $this->info ( 'name' );
		$select = $this->select ()->where ( $sitepageName . '.page_id <> ?', $page_id )->where ( $sitepageName . '.owner_id  =?', $viewer_id )->where ( 'NOT EXISTS (SELECT `page_id` FROM `engine4_sitepage_favourites` WHERE `page_id_for`=' . $page_id . ' AND `page_id` = ' . $sitepageName . '.`page_id`) ' );
		
		return $this->fetchALL ( $select )->toArray ();
	}
	
	public function sitepageselect($page_id) {
		$sitepageselect = $this->select ()->where ( 'page_id =?', $page_id );
		return $userPages = $this->fetchALL ( $sitepageselect );
	}
	
	/**
	 * Get Discussed Pages
	 *
	 * @return all discussed pages
	 */
	public function getDiscussedPage() {
		$sitepage_tableName = $this->info ( 'name' );
		$topic_tableName = Engine_Api::_ ()->getDbTable ( 'topics', 'sitepage' )->info ( 'name' );
		$select = $this->select ()->setIntegrityCheck ( false )->from ( $sitepage_tableName )->join ( $topic_tableName, $topic_tableName . '.page_id = ' . $sitepage_tableName . '.page_id', array ('count(*) as counttopics', '(sum(post_count) - count(*) ) as total_count' ) )->where ( $sitepage_tableName . '.closed = ?', '0' )->where ( $sitepage_tableName . '.approved = ?', '1' )->where ( $sitepage_tableName . '.draft = ?', '1' )->where ( $topic_tableName . '.post_count > ?', '1' )->group ( $topic_tableName . '.page_id' )->order ( 'total_count DESC' )->order ( 'counttopics DESC' )->limit ( Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.mostdiscussed.widgets', 4 ) );
		if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ())
			$select->where ( $sitepage_tableName . '.expiration_date  > ?', date ( "Y-m-d H:i:s" ) );
		$select->where ( $sitepage_tableName . ".search = ?", 1 );
		
		//START NETWORK WORK
		$enableNetwork = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.network', 0 );
		if (! empty ( $enableNetwork )) {
			$viewer = Engine_Api::_ ()->user ()->getViewer ();
			$networkMembershipTable = Engine_Api::_ ()->getDbtable ( 'membership', 'network' );
			if (! Engine_Api::_ ()->getApi ( 'subCore', 'sitepage' )->pageBaseNetworkEnable ()) {
				$viewerNetwork = $networkMembershipTable->fetchRow ( array ('user_id = ?' => $viewer->getIdentity () ) );
				
				if (! empty ( $viewerNetwork )) {
					
					$networkMembershipName = $networkMembershipTable->info ( 'name' );
					$select->join ( $networkMembershipName, "`{$sitepage_tableName}`.owner_id = `{$networkMembershipName}`.user_id  ", null )->join ( $networkMembershipName, "`{$networkMembershipName}`.`resource_id`=`{$networkMembershipName}_2`.resource_id", null )->where ( "`{$networkMembershipName}_2`.user_id = ? ", $viewer->getIdentity () );
				}
			} else {
				$viewerNetwork = $networkMembershipTable->getMembershipsOfInfo ( $viewer );
				$str = array ();
				$columnName = "`{$sitepage_tableName}`.networks_privacy";
				foreach ( $viewerNetwork as $networkvalue ) {
					$network_id = $networkvalue->resource_id;
					$str [] = "'" . $network_id . "'";
					$str [] = "'" . $network_id . ",%'";
					$str [] = "'%," . $network_id . ",%'";
					$str [] = "'%," . $network_id . "'";
				}
				if (! empty ( $str )) {
					$likeNetworkVale = ( string ) (join ( " or $columnName  LIKE ", $str ));
					$select->where ( $columnName . ' LIKE ' . $likeNetworkVale . ' or ' . $columnName . " IS NULL" );
				} else {
					$select->where ( $columnName . " IS NULL" );
				}
			}
		}
		//END NETWORK WORK
		return $this->fetchALL ( $select );
	}
	
	/**
	 * Get pages based on category
	 * @param string $title : search text
	 * @param int $category_id : category id
	 * @param char $popularity : result sorting based on views, reviews, likes, comments
	 * @param char $interval : time interval
	 * @param string $sqlTimeStr : Time durating string for where clause 
	 */
	public function pagesByCategory($category_id, $popularity, $interval, $sqlTimeStr, $totalPages = 5) {
		
		$pageTableName = $this->info ( 'name' );
		
		if ($interval == 'overall' || $popularity == 'view_count') {
			
			$select = $this->select ()->from ( $pageTableName, array ('page_id', 'title', 'photo_id', 'page_url', 'owner_id' ) )->where ( $pageTableName . '.category_id = ?', $category_id )->where ( $pageTableName . '.closed = ?', '0' )->where ( $pageTableName . '.approved = ?', '1' )->where ( $pageTableName . '.declined = ?', '0' )->where ( $pageTableName . '.draft = ?', '1' )->order ( "$popularity DESC" )->order ( "creation_date DESC" )->limit ( $totalPages );
			if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
				$select->where ( $pageTableName . '.expiration_date  > ?', date ( "Y-m-d H:i:s" ) );
			}
		} elseif ($popularity == 'review_count' && $interval != 'overall') {
			
			$popularityTable = Engine_Api::_ ()->getDbtable ( 'reviews', 'sitepagereview' );
			$popularityTableName = $popularityTable->info ( 'name' );
			
			$select = $this->select ()->setIntegrityCheck ( false )->from ( $pageTableName, array ('page_id', 'title', 'photo_id', 'page_url', 'owner_id' ) )->joinLeft ( $popularityTableName, $popularityTableName . '.page_id = ' . $pageTableName . '.page_id', array ("COUNT(review_id) as total_count" ) )->where ( $pageTableName . '.category_id = ?', $category_id )->where ( $pageTableName . '.closed = ?', '0' )->where ( $pageTableName . '.approved = ?', '1' )->where ( $pageTableName . '.declined = ?', '0' )->where ( $pageTableName . '.draft = ?', '1' )->where ( $popularityTableName . "$sqlTimeStr  or " . $popularityTableName . '.creation_date is null' )->group ( $pageTableName . '.page_id' )->order ( "total_count DESC" )->order ( $pageTableName . ".creation_date DESC" )->limit ( $totalPages );
			if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
				$select->where ( $pageTableName . '.expiration_date  > ?', date ( "Y-m-d H:i:s" ) );
			}
		} elseif ($popularity != 'view_count' && $popularity != 'review_count' && $interval != 'overall') {
			
			if ($popularity == 'like_count') {
				$popularityType = 'like';
			} else {
				$popularityType = 'comment';
			}
			
			$id = $popularityType . "_id";
			
			$popularityTable = Engine_Api::_ ()->getDbtable ( "$popularityType" . "s", 'core' );
			$popularityTableName = $popularityTable->info ( 'name' );
			
			$select = $this->select ()->setIntegrityCheck ( false )->from ( $pageTableName, array ('page_id', 'title', 'photo_id', 'page_url', 'owner_id' ) )->joinLeft ( $popularityTableName, $popularityTableName . '.resource_id = ' . $pageTableName . '.page_id', array ("COUNT($id) as total_count" ) )->where ( $pageTableName . '.category_id = ?', $category_id )->where ( $pageTableName . '.closed = ?', '0' )->where ( $pageTableName . '.approved = ?', '1' )->where ( $pageTableName . '.declined = ?', '0' )->where ( $pageTableName . '.draft = ?', '1' )->where ( $popularityTableName . "$sqlTimeStr  or " . $popularityTableName . '.creation_date is null' )->group ( $pageTableName . '.page_id' )->order ( "total_count DESC" )->order ( $pageTableName . ".creation_date DESC" )->limit ( $totalPages );
			if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
				$select->where ( $pageTableName . '.expiration_date  > ?', date ( "Y-m-d H:i:s" ) );
			}
		}
		
		return $this->fetchAll ( $select );
	}
	
	public function checkPage() {
		return $this->fetchALL ( $this->select () )->toArray ();
	}
	public function getItemOfDay() {
		
		//$sitepageTable = Engine_Api::_()->getDbtable('pages', 'sitepage');
		$sitepageTableName = $this->info ( 'name' );
		
		$itemofthedaytable = Engine_Api::_ ()->getDbtable ( 'itemofthedays', 'sitepage' );
		$itemofthedayName = $itemofthedaytable->info ( 'name' );
		
		$select = $this->select ();
		$select = $select->setIntegrityCheck ( false )->from ( $sitepageTableName )->join ( $itemofthedayName, $sitepageTableName . ".page_id = " . $itemofthedayName . '.resource_id', array ('start_date' ) )->where ( $itemofthedayName . '.resource_type=?', 'sitepage_page' )->where ( $itemofthedayName . '.start_date <=?', date ( 'Y-m-d' ) )->where ( $itemofthedayName . '.end_date >=?', date ( 'Y-m-d' ) )->order ( 'RAND()' );
		return $this->fetchRow ( $select );
	}

}
?>